package com.xunk.system.service.impl;

import com.xunk.common.core.utils.DataUtils;
import com.xunk.common.core.utils.DateUtils;
import com.xunk.common.security.utils.SecurityUtils;
import com.xunk.system.config.BuzConfig;
import com.xunk.system.domain.SysDeviceBaseInfo;
import com.xunk.system.mapper.SysDeviceBaseInfoMapper;
import com.xunk.system.mapper.SysDeviceMapper;
import com.xunk.system.service.ISysDeviceBaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 设备基础信息，按类别存储Service业务层处理
 *
 * @author xunk
 * @date 2021-04-25
 */
@Service
public class SysDeviceBaseInfoServiceImpl implements ISysDeviceBaseInfoService {
    @Autowired
    private SysDeviceBaseInfoMapper sysDeviceBaseInfoMapper;

    @Autowired
    SysDeviceMapper deviceMapper;

    @Autowired
    BuzConfig buzConfig;

    /**
     * 查询设备基础信息，按类别存储
     *
     * @param id 设备基础信息，按类别存储ID
     * @return 设备基础信息，按类别存储
     */
    @Override
    public SysDeviceBaseInfo selectSysDeviceBaseInfoById(Long id) {
        SysDeviceBaseInfo baseInfo = sysDeviceBaseInfoMapper.selectSysDeviceBaseInfoById(id);
        if (DataUtils.isNotEmpty(baseInfo.getIcon())) {
            baseInfo.setIcon(buzConfig.getImageServerIp() + baseInfo.getIcon());
        }
        return baseInfo;
    }

    /**
     * 查询设备基础信息，按类别存储列表
     *
     * @param sysDeviceBaseInfo 设备基础信息，按类别存储
     * @return 设备基础信息，按类别存储
     */
    @Override
    public List<SysDeviceBaseInfo> selectSysDeviceBaseInfoList(SysDeviceBaseInfo sysDeviceBaseInfo) {
        List<SysDeviceBaseInfo> sysDeviceBaseInfos = sysDeviceBaseInfoMapper.selectSysDeviceBaseInfoList(sysDeviceBaseInfo);
        // 查询当前产品下挂载了多少设备
        List<Long> productIds = sysDeviceBaseInfos.stream().peek(item -> {
            if (DataUtils.isNotEmpty(item.getIcon())) {
                item.setIcon(buzConfig.getImageServerIp() + item.getIcon());
            }
        }).map(SysDeviceBaseInfo::getId).collect(Collectors.toList());
        List<HashMap<Long, Long>> hashMaps = deviceMapper.countDeviceByBaseId(productIds);
        Map<Long, Long> map = hashMaps.stream().collect(Collectors.toMap(a -> a.get("id"), a -> a.get("count")));
        sysDeviceBaseInfos.forEach(item -> {
            item.setDeviceCount(map.getOrDefault(item.getId(), 0L));
        });
        return sysDeviceBaseInfos;
    }

    /**
     * 新增设备基础信息，按类别存储
     *
     * @param sysDeviceBaseInfo 设备基础信息，按类别存储
     * @return 结果
     */
    @Override
    public int insertSysDeviceBaseInfo(SysDeviceBaseInfo sysDeviceBaseInfo) {
        sysDeviceBaseInfo.setCreateTime(DateUtils.getNowDate());
        sysDeviceBaseInfo.setCreateBy(Optional.ofNullable(SecurityUtils.getUserId()).map(String::valueOf).orElse(""));
        return sysDeviceBaseInfoMapper.insertSysDeviceBaseInfo(sysDeviceBaseInfo);
    }

    /**
     * 修改设备基础信息，按类别存储
     *
     * @param sysDeviceBaseInfo 设备基础信息，按类别存储
     * @return 结果
     */
    @Override
    public int updateSysDeviceBaseInfo(SysDeviceBaseInfo sysDeviceBaseInfo) {
        sysDeviceBaseInfo.setUpdateTime(DateUtils.getNowDate());
        sysDeviceBaseInfo.setUpdateBy(Optional.ofNullable(SecurityUtils.getUserId()).map(String::valueOf).orElse(""));
        return sysDeviceBaseInfoMapper.updateSysDeviceBaseInfo(sysDeviceBaseInfo);
    }

    /**
     * 批量删除设备基础信息，按类别存储
     *
     * @param ids 需要删除的设备基础信息，按类别存储ID
     * @return 结果
     */
    @Override
    public int deleteSysDeviceBaseInfoByIds(Long[] ids) {
        return sysDeviceBaseInfoMapper.deleteSysDeviceBaseInfoByIds(ids);
    }

    /**
     * 删除设备基础信息，按类别存储信息
     *
     * @param id 设备基础信息，按类别存储ID
     * @return 结果
     */
    @Override
    public int deleteSysDeviceBaseInfoById(Long id) {
        return sysDeviceBaseInfoMapper.deleteSysDeviceBaseInfoById(id);
    }
}
